package com.maykk.Demo02;

import java.sql.*;

/**
 * @ClassName: com.maykk.Demo02.JdbcUtils
 * @Description: TODO 静态方法封装 JDBC 工具类
 * @Author: MaykkStar@163.com
 */
public class JdbcUtils {
    private static String URL = "jdbc:mysql://localhost:3306/maykk?useSSL=false";
    private static String USER = "root";
    private static String PWD = "123456";

    private JdbcUtils() {} // 私有化构造方法，防止在类外实例化

    // 注册驱动
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    // 获取执行对象
    public static Connection getConnection() {
        try {
            return DriverManager.getConnection(URL,USER,PWD);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    public static void free(ResultSet rs, Statement st, Connection conn) {
        try {
            if (null != rs)
                rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            try {
                if(null != st)
                    st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                    if (null != conn)
                        conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
